{include file="public/header-static"/}
{include file="public/sidebar"/}
<div id="main">

    {include file="public/header"/}

    <main class="main-content">

        <div class="container">

            <div class="row jub">
                <div class="col-md-8">
                    <div class="card" style="padding-bottom: 16px">


                        <div class="d-lxe">

                            <div class="z-left">
                                <div class="unm-s">4053</div>
                                <div class="ubn-str">总营收</div>
                                <div class="qb-buttom">查看全部</div>
                            </div>
                            <div class="col-md-7">
                                <div class="z-head">
                                    <div class="z-tab" onclick="catable(1)">每天</div>
                                    <div class="z-tab">每周</div>
                                    <div class="z-tab">每月</div>
                                    <div class="z-tab">每年</div>
                                </div>
                                <div>
                                    <canvas id="myChart" style="height: 200px"></canvas>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div>
                            <canvas id="post"></canvas>
                        </div>
                        <div class="mat-top">
                            <div class="communiy">上月增长数：100</div>
                            <div class="communiy">环比增长数：200</div>
                        </div>

                    </div>
                </div>
            </div>
            <div class="app-block">
                <div class="app-content">
                    <div class="app-action">
                        <div class="action-left">
                            <ul class="list-inline">
                                <li class="list-inline-item mb-0">
                                    <a href="javascript:void(0);" onclick="add()" class="btn btn-secondary ">
                                        <i data-feather="plus" class="mr-2"></i>
                                        添加                                                                                                      </a>
                                </li>

                            </ul>
                        </div>
                        <div class="action-right">
                            <a href="#" class="app-sidebar-menu-button btn btn-outline-light">
                                <i data-feather="menu"></i>
                            </a>
                            <div class="input-group">
                                <input type="text" name="keywords" id="keywords" autofocus="autofocus" value="{$Request.get.keywords}"  class="form-control" placeholder="搜索关键字[社群名称|创建人]"
                                       aria-describedby="button-addon1">
                                <div class="input-group-append">
                                    <button class="btn btn-outline-light" type="submit" id="button-addon1">
                                        <i data-feather="search"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {include file="public/page-header"/}
            <!-- end::page-header -->
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-body">

                            <table id="linkstable" lay-filter="linkstable"></table>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </main>
    {include file="public/footer"/}
</div>
<style>
    #dis{background-color:#d8d6d6;pointer-events: none;}
</style>

<script type="text/html" id="switchTpl">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="switch_status" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="countTpl">
    <span>点击：{{ d.click }} </span><br>
    <span style="color:#23c6c8">收藏：{{ d.collect }} </span><br>
    <span>分享：{{ d.share }} </span>
</script>

<script type="text/html" id="cateTpl">
    <span>{{ d.cateInfo }}</span>
</script>

<script type="text/html" id="barDemo">
    <button data-toggle="dropdown" class="btn btn-sm btn-outline-secondary">推荐</button><br>
    <ul class="dropdown-menu">
        <li class="dropdown-item" {{# if(d.search < 1){ }} onclick="recommend({{ d.id }}, 1)" {{# } else { }} disabled id="dis"{{# } }}>检索栏</li>
        <li class="dropdown-item" {{# if(d.fixed < 1){ }} onclick="recommend({{ d.id }}, 2)" {{# } else { }} disabled id="dis"{{# } }}>固定栏</li>
    </ul>
    <button lay-event="see" class="btn btn-sm btn-outline-secondary">详情</button><br>
    <button lay-event="edit" class="btn btn-sm btn-outline-secondary">编辑</button><br>
    <button lay-event="del" class="btn btn-sm btn-outline-secondary">删除</button>
</script>

<script src="__STATIC__/assets/js/custom.js"></script>

<script>
    new Chart(document.getElementById("post"), {
        type: 'doughnut',
        data: {
            labels: ["本周", "昨日", "实时"],
            datasets: [
                {
                    label: "Population (millions)",
                    backgroundColor: ["#97D70D", "#F69136","#347AF7"],
                    data: [100,100,100]

                }
            ]
        },

        options: {

            title: {

                display: true,

                text: '帖子统计'

            }

        }

    });

    $(function () {

        var ctx = document.getElementById("myChart").getContext('2d');
        var data = {
            labels : ["1","2","3","4","5","6"],
            datasets : [
                {
                    label:'2017',
                    backgroundColor:'rgba(40,161, 121, 0.1)',
                    borderColor: "#36A2EB", //路径颜色
                    pointBackgroundColor: "#1BB8FA", //数据点颜色
                    pointBorderColor: "#fff", //数据点边框颜色
                    data : [0,15,10,35,10,0],
                    borderWidth:'2',
                    pointRadius:0

                },

            ],

        }
        var myLineChart = new Chart(ctx, {
            type: 'line',
            data: data,
            options: {
                scales: {
                    yAxes: [{
                        tension: 0,
                    }]
                }
            }
        });



    });

    function recommend(id,type) {
        console.log(id,type);
        var typeName = type == 1 ? '检索栏' : '固定栏';
        layer.confirm('推荐到'+typeName+'?', {icon: 3, title:'提示'}, function(index) {
            $.post("{:url('/console/addrecommend')}", {id: id, type: type}, function (res) {
                // console.log(res)
                if (res['code'] != 1) {
                    toastr.error(res['msg']);
                } else {
                    window.location.reload();
                }
            });
            layer.close(index);
        });
    }
    function add() {
        window.location.href = '{:url("/console/resourceAdd")}';
    }
    function edit(id) {
        window.location.href = '{:url("/console/resourceEdit")}?id='+id;
    }
    function see(id) {
        window.location.href = '{:url("/console/resourceShow")}?id='+id;
    }
    function del(id) {
        $.post("{:url('/console/resourceDel')}", {id:id}, function(data) {
            if(data['code']!=1) {
                toastr.error(data['msg']);
            }else{
                window.location.reload();
            }
        });
    }

    // layui插件
    layui.use('table', function(){
        var table = layui.table
            ,form = layui.form;
        // 表格初始化
        table.render({
            elem: '#linkstable'
            ,url: '{:url("/console/index")}'
            ,method:'post'
            ,page: true //开启分页
            ,cols: [[
                {field:'id', title:'ID'}
                ,{field:'name', title:'社群名称'}
                ,{field:'founder', title:'创建人'}
                ,{field:'images', title:'封面', width:80, templet: function(d) { return '<div onclick="show_img(this)" ><img src="'+d.images+'" ' + 'alt="" width="60px" height="60px"></a></div>'; }}
                ,{field:'qrcode', title:'群二维码', width:80, templet: function(d) { return '<div onclick="show_img(this)" ><img src="'+d.qrcode+'" ' + 'alt="" width="60px" height="60px"></a></div>'; }}
                ,{field:'cate', title:'分类', templet: '#cateTpl', unresize: true}
                ,{field:'count', title:'统计', templet: '#countTpl', unresize: true}
                ,{field:'status', title:'状态', templet: '#switchTpl', unresize: true}
                ,{field:'create_time', title:'创建时间'}
                ,{field:'rightbtn', title:'操作', toolbar: '#barDemo'}
            ]],
            done:function (res) {
                var element = $('td[data-field="rightbtn"]').children('.layui-table-cell');
                element.css('overflow','visible');
                element.children('.dropdown-menu').css('min-width','auto');
            },
            parseData: function(res){
                return {
                    "code": res.data.code,
                    "msg": res.data.msg,
                    "count": res.data.count,
                    "data": res.data.list
                };
            }
        });
        table.on('tool(linkstable)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('删除该数据？', {icon: 3, title:'提示'}, function(index){
                    obj.del();
                    layer.close(index);
                    del(data.id);
                });
            }else if(obj.event === 'edit'){
                edit(data.id);
            }else if(obj.event === 'see'){
                see(data.id);
            }
        });

        // 搜索
        $('#button-addon1').on('click', function(){
            var keywords= $("#keywords").val();
            table.reload('linkstable', {
                url: '{:url("/console/index")}'
                ,where: {
                    keywords : keywords // 搜索参数
                }
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        // 状态
        form.on('switch(switch_status)', function(obj){
            $.post("{:url('/console/resourceChangeStatus')}",{id:obj.value,field:'status'},function(){});
        });
    });

    // 图片放大
    function show_img(element) {
        var elements = $(element).find("img");
        //页面层
        layer.open({
            type: 1,
            title: '图片',
            anim: 2,
            area: ['800px','500px'], //宽高 t.width() t.height()
            shadeClose: true, //开启遮罩关闭
            end: function (index, layero) {
                return false;
            },
            content: '<div style="text-align:center"><img src="' + $(elements).attr('src') + '" /></div>'
        });
    }
</script>